import { createRouter, createWebHashHistory } from 'vue-router'
import {useUserStore} from '@/stores/userStore'
   
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: 'login'
    },
    {
      path:'/index.html',
      redirect:'/login'
    },
    {
      path: '/layout',
      name: 'layout',
      redirect:'/home',
      component: () => import('../views/LayoutView.vue'),
      children:[
        {
          path: '/me',
          name: 'me',
          component: () => import('../views/Me.vue')
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/Home.vue')
        },
        {
          path: '/cart',
          name: 'cart',
          component: () => import('../views/Cart.vue')
        },
        {
          path: '/order',
          name: 'order',
          component: () => import('../views/Order.vue')
        },
      ]
    },
    {
      path: '/detail/:proid',
      name: 'detail',
      component: () => import('../views/Detail.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/regist',
      name: 'regist',
      component: () => import('../views/RegistView.vue')
    },
  ]
})
let whiteList = ['/', '/home', '/layout', '/login', '/reg', '/detail'];
router.beforeEach((to, from) => {
  if(!whiteList.includes(to.path)){
    let userStore = useUserStore();
    if(userStore.token == ""){
      router.push("/login");
    }
  }
})
export default router
